<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class login extends CI_Model {
    
	function __construct() 
	{
		parent::__construct();
            $this->load->model('dbase');
	}
        
        function session_Check()
        {
            $session_id = $this->session->userdata('session_id');
            
            if($session_id != FALSE && $this->session->userdata('logged_in') == TRUE)
            {
                $steamid = $this->session->userdata['steamid'];
                $userid = $this->dbase->userinfo($steamid);
                $row = $userid->row();
                
                $data = array(
                    'id' => $row->id,
                    'nickname' => $row->nickname,
                    'avatar' => $row->avatar,
                );
                
                return $data;
                
            } else if($this->session->userdata('logged_in') == FALSE) {
                $this->load->model('steamopenid');
                              
                $steam_userid64 = $this->steamopenid->validate();
                
                if(!empty($steam_userid64))
                {
                    $this->session->set_userdata('steamid', $steam_userid64);
                    $userid = $this->dbase->userinfo($steam_userid64);
                    $this->session->set_userdata('logged_in', TRUE);
                    
                    if($userid->num_rows() == 0)
                    {
                        $steam_api_key = "E7DA0FD41903BCF3CE5F78381011B2A7";
                        $user_json = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$steam_api_key."&steamids=".$steam_userid64);
                        $user = json_decode($user_json);
                    
                        $username = $user->response->players[0]->personaname;
                        $avatar = $user->response->players[0]->avatar;
                    
                        $data = array(
                            'steamid' => $steam_userid64,
                            'nickname' => $username,
                            'avatar' => $avatar,
                        );
                    $this->dbase->userupdate($data);
                    return $data;
                    } else {                        
                        $steamid = $this->session->userdata['steamid'];
                        $userid = $this->dbase->userinfo($steamid);
                        return $userid->result_array();
                    }
                } else {
                    $this->session->set_userdata('logged_in', FALSE);
                    return FALSE;
                }
            }
        }
}


/* End of file $sessions.php */
/* Location: ./application/models/sessions.php */
